package view;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import main.KeyDefinitions;

public class EditControlPrompt extends JFrame{
	private EditControlLabel lblPrompt;
	private EditControlLabel lblKey;
	private EditControlLabel lblKeyText;
	private EditControlButton btnSubmit;
	private EditControlButton btnCancel;
	int key[] = new int[2];
	String command;
	KeyDefinitions keyDefs;
	EditControlView ecv;
	public EditControlPrompt(String _command,KeyDefinitions _keyDefs,EditControlView _ecv){
		command = _command;
		keyDefs = _keyDefs;
		ecv = _ecv;
		initComps();
		this.setVisible(true);
		this.getContentPane().setBackground(new Color(157,157,157));
		this.setSize(235,125);
		this.setTitle("Edit Control Prompt");
	}
	private void initComps(){
		GridBagConstraints gbc = new GridBagConstraints();
		this.getContentPane().setLayout(new GridBagLayout());
		this.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                changeKeyText(e);
            }
        });
		
		//lblPrompt
		lblPrompt = new EditControlLabel("Press New Key");
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.fill = java.awt.GridBagConstraints.BOTH;
        this.getContentPane().add(lblPrompt, gbc);
        
        //lblKey
        lblKey = new EditControlLabel(command);
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.fill = java.awt.GridBagConstraints.BOTH;
		this.getContentPane().add(lblKey, gbc);
		
        //lblKeyText
        lblKeyText = new EditControlLabel("**********");
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.fill = java.awt.GridBagConstraints.BOTH;
		this.getContentPane().add(lblKeyText, gbc);
		
        //btnSubmit
        btnSubmit = new EditControlButton("Submit");
        btnSubmit.setFocusable(false);
        btnSubmit.addMouseListener(new MouseAdapter(){
        	public void mouseClicked(java.awt.event.MouseEvent evt) {
                submitClicked();
            }
        });
        gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.fill = java.awt.GridBagConstraints.BOTH;
		this.getContentPane().add(btnSubmit, gbc);
		
        //btnCancel
        btnCancel = new EditControlButton("Cancel");
        btnCancel.setFocusable(false);
        btnCancel.addMouseListener(new MouseAdapter(){
        	public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelClicked();
            }
        });
        gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.fill = java.awt.GridBagConstraints.CENTER;
		this.getContentPane().add(btnCancel, gbc);
	}
	private void changeKeyText(KeyEvent e){
		key[0] = e.getKeyCode();
		key[1] = e.getModifiers();
		lblKeyText.lblText = (KeyEvent.getKeyModifiersText(key[1]) + " " + KeyEvent.getKeyText(key[0]));
		lblKeyText.repaint();
	}
	public void cancelClicked(){
	
		ecv.updateButtonText();
		this.dispose();
	}
	public void submitClicked(){
		if(command.equals("modePrev")){keyDefs.setModePrev(key);}
        else if(command.equals("modeNext")){keyDefs.setModeNext(key);}
        else if(command.equals("typePrev")){keyDefs.setTypePrev(key);}
        else if(command.equals("typeNext")){keyDefs.setTypeNext(key);}
        else if(command.equals("instancePrev")){keyDefs.setInstancePrev(key);}
        else if(command.equals("instanceNext")){keyDefs.setInstanceNext(key); }
        else if(command.equals("commandPrev")){keyDefs.setCommandPrev(key);}
        else if(command.equals("commandNext")){keyDefs.setCommandNext(key); }
        else if(command.equals("North")){keyDefs.setNorth(key);}
        else if(command.equals("NorthEast")){keyDefs.setNorthEast(key);}
        else if(command.equals("NorthWest")){keyDefs.setNorthWest(key);}
        else if(command.equals("South")){keyDefs.setSouth(key);}
        else if(command.equals("SouthEast")){keyDefs.setSouthEast(key);}
        else if(command.equals("SouthWest")){keyDefs.setSouthWest(key);}
        else if(command.equals("Select1")){keyDefs.setSelect1(key);}
        else if(command.equals("Select2")){keyDefs.setSelect2(key);}
        else if(command.equals("Select3")){keyDefs.setSelect3(key);}
        else if(command.equals("Select4")){keyDefs.setSelect4(key);}
        else if(command.equals("Select5")){keyDefs.setSelect5(key);}
        else if(command.equals("Select6")){keyDefs.setSelect6(key);}
        else if(command.equals("Select7")){keyDefs.setSelect7(key);}
        else if(command.equals("Select8")){keyDefs.setSelect8(key);}
        else if(command.equals("Select9")){keyDefs.setSelect9(key);}
        else if(command.equals("Select0")){keyDefs.setSelect0(key);}
        else if(command.equals("Focus")){keyDefs.setFocus(key);}
        else if(command.equals("Issue")){keyDefs.setIssue(key);}
        else if(command.equals("Pause")){keyDefs.setPause(key);}
        else if(command.equals("Tab")){keyDefs.setTab(key);}
        else if(command.equals("createRally")){keyDefs.setCreateRally(key);}
        else if(command.equals("Cancel")){keyDefs.setCancel(key);}
        else if(command.equals("switchView")){keyDefs.setSwitchView(key);}
		ecv.updateButtonText();
		this.dispose();
	}
}
